home *** CD-ROM | disk | FTP | other *** search
- /* 4567890123456789012345678901234567890123456789012345678901234567 */
- #include <Errors.h>
- #include <Folders.h>
- #include <Devices.h>
- #include <TextUtils.h>
-
- #include "EvenMoreFiles.h"
-
- #include "Toolbox.h"
-
- pascal OSErr GetFileSpec(short anFRefNum, FSSpec *aFileSpec) {
- OSErr theError;
- FCBPBRec pb;
- Str255 theName;
- short theVRefNum;
- long theDirID;
-
- pb.ioRefNum = anFRefNum; /* the file to target */
- pb.ioNamePtr = theName; /* return the name */
- pb.ioFCBIndx = 0; /* not indexing */
- pb.ioVRefNum = 0; /* all open files */
- theError = PBGetFCBInfoSync(&pb);
- if (theError == noErr) {
- theDirID = pb.ioFCBParID;
- theVRefNum = pb.ioFCBVRefNum;
- theError =
- FSMakeFSSpec(theVRefNum, theDirID, theName, aFileSpec);
- }
- return theError;
- }
-
- pascal OSErr GetVolumeModDate(short aVRefNum, unsigned long *aDate) {
- OSErr theError = noErr;
- HParamBlockRec pb;
-
- pb.volumeParam.ioVRefNum = aVRefNum; /* the target volume */
- pb.volumeParam.ioVolIndex = 0; /* Use vRefNum */
- pb.volumeParam.ioNamePtr = nil; /* Don't care about name */
- theError = PBHGetVInfoSync(&pb);
-
- *aDate = pb.volumeParam.ioVLsMod;
- return theError;
- }
-
- pascal OSErr GetNameOfReferencedFile(short anFRefNum, Str255 aName) {
- FCBPBRec pb;
- OSErr theError = noErr;
-
- pb.ioRefNum = anFRefNum; /* the target file */
- pb.ioNamePtr = aName; /* return the name */
- pb.ioFCBIndx = 0; /* not indexing */
- pb.ioVRefNum = 0; /* all open files */
- theError = PBGetFCBInfoSync(&pb);
- return theError;
- }
-
- pascal OSErr GetFilesVolume(short anFRefNum, short *aVRefNum) {
- FCBPBRec pb;
- OSErr theError = noErr;
-
- pb.ioRefNum = anFRefNum; /* the target file */
- pb.ioNamePtr = nil; /* do not care about file */
- pb.ioFCBIndx = 0; /* not indexing */
- pb.ioVRefNum = 0; /* all open files */
- theError = PBGetFCBInfoSync(&pb);
- if (theError == noErr) {
- *aVRefNum = pb.ioFCBVRefNum;
- }
- return theError;
- }
-
- pascal OSErr GetFileParID(short aRefNum, long *aParID) {
- OSErr theError;
- FCBPBRec pb;
-
- pb.ioRefNum = aRefNum; /* the target file */
- pb.ioNamePtr = nil; /* do not care about name */
- pb.ioFCBIndx = 0; /* not indexing */
- pb.ioVRefNum = 0; /* all open files */
- theError = PBGetFCBInfoSync(&pb);
- if (theError == noErr) {
- *aParID = pb.ioFCBParID;
- }
- return theError;
- }
-
- pascal OSErr GetFolderParID(short aVRefNum, long aDirID,
- long *aParID) {
- OSErr theError;
- CInfoPBRec pb;
-
- pb.dirInfo.ioVRefNum = aVRefNum; /* the target volume */
- pb.dirInfo.ioDrDirID = aDirID; /* the target directory */
- pb.dirInfo.ioFDirIndex = -1; /* ioDrDirID specifies target */
- pb.dirInfo.ioNamePtr = nil; /* do not care about name */
- theError = PBGetCatInfoSync(&pb);
- if (theError == noErr) {
- *aParID = pb.dirInfo.ioDrParID;
- }
- return theError;
- }
-
- pascal OSErr GetFolderName(short aVRefNum, long aDirID, Str255 aName) {
- OSErr theError;
- CInfoPBRec pb;
-
- pb.dirInfo.ioVRefNum = aVRefNum; /* the target volume */
- pb.dirInfo.ioDrDirID = aDirID; /* the target directory */
- pb.dirInfo.ioFDirIndex = -1; /* ioDrDirID specifies target */
- pb.dirInfo.ioNamePtr = aName; /* do not care about name */
- theError = PBGetCatInfoSync(&pb);
- return theError;
- }
-
- pascal OSErr GetVolumeName(short aVRefNum, Str255 aName) {
- OSErr theError;
- CInfoPBRec pb;
-
- pb.dirInfo.ioNamePtr = aName; /* return the name */
- pb.dirInfo.ioVRefNum = aVRefNum; /* the target volume */
- pb.dirInfo.ioDrDirID = fsRtDirID; /* volume name == root name */
- pb.dirInfo.ioFDirIndex = -1; /* ioDrDirID specifies target */
- theError = PBGetCatInfoSync(&pb);
- return theError;
- }
-
- pascal OSErr CanAccess(short aVRefNum, long aDirID,
- Boolean *canAccess) {
- OSErr theError;
- CInfoPBRec pb;
-
- pb.dirInfo.ioNamePtr = nil; /* don't care about name */
- pb.dirInfo.ioVRefNum = aVRefNum;
- pb.dirInfo.ioDrDirID = aDirID;
- pb.dirInfo.ioFDirIndex = -1; /* ioDrDirID specifies target */
- /*
- * An obscure note in Inside Macintosh: Files on page 2-192 says,
- * "The PBGetCatInfo function returns the directory access rights
- * in the ioACUser field only for shared volumes. As a result, you
- * should set this field to 0 before calling PBGetCatInfo."
- *
- * What this note really means is that PBGetCatInfo fills in the
- * ioACUser field with the file access rights on shared volumes
- * and does nothing with this field on non-shared volumes. There
- * is a happy coincidence that on non-shared volumes you always
- * have full access to all files. Furthermore, the ioACUser code
- * for full access is zero (0).
- *
- * So... If you fill in the ioACUser field ahead of time with zero
- * then after the call the field value will be correct EVEN if the
- * file is not on a shared volume.
- *
- * IF YOU ARE WITH ME SO FAR...
- *
- * You may have noticed that attempting to set the ioACUser field
- * of the DirInfo portion of a CInfoPBRec you will get a compile
- * error. This is because this field was cleverly named 'filler2'.
- *
- * Thanks to Jim Luther for helping me through this bit of
- * cleverness--I don't think most developers get this kind of help!
- */
- pb.dirInfo.filler2 = 0; /* really is ioACUser field */
- theError = PBGetCatInfoSync(&pb);
- if (theError == noErr) {
- pb.dirInfo.filler2 &= 0x7f; /* don't care if user is owner */
- if (pb.dirInfo.filler2 == 0) { /* really is ioACUser field */
- *canAccess = true;
- } else {
- *canAccess = false;
- }
- }
- return theError;
- #if 0
- /*
- * This is the less clever way I was doing it originally which
- * required an additional line of code for the sake of clarity.
- */
- #define kUserWrite 0x04000000L
- #define kUserRead 0x02000000L
-
- OSErr theError;
- HParamBlockRec pb;
-
- pb.accessParam.ioNamePtr = nil; /* don't care about name */
- pb.accessParam.ioVRefNum = aVRefNum;
- pb.fileParam.ioDirID = aDirID;
- theError = PBHGetDirAccessSync(&pb);
- if (theError == noErr) {
- Boolean noWriteAccess =
- ((pb.accessParam.ioACAccess & kUserWrite) == 0L);
- Boolean noReadAccess =
- ((pb.accessParam.ioACAccess & kUserRead) == 0L);
-
- if (noWriteAccess || noReadAccess) {
- *canAccess = false;
- } else {
- *canAccess = true;
- }
- } else if (theError == paramErr) {
- /*
- * Volume does not support directory access
- */
- *canAccess = true;
- theError = noErr;
- }
- return theError;
- #endif
- }
-
- pascal OSErr FileInTrashCan(short aFRefNum, Boolean *inTrash) {
- OSErr theError;
- short theVRefNum;
- short theTrashVRefNum;
- long theTrashDirID;
- long theDirID;
-
- theError = GetVRefNum(aFRefNum, &theVRefNum);
- if (theError == noErr) {
- theError = FindFolder(theVRefNum, kTrashFolderType, false,
- &theTrashVRefNum, &theTrashDirID);
- if (theError == fnfErr) {
- theTrashDirID = 0;
- theError = noErr;
- }
- }
- if (theError == noErr) {
- Boolean done = false;
-
- theError = GetFileParID(aFRefNum, &theDirID);
- while ((!done) && (theError == noErr)) {
- if (theDirID == theTrashDirID) {
- *inTrash = true;
- done = true;
- } else if (theDirID == fsRtDirID) {
- *inTrash = false;
- done = true;
- } else {
- theError =
- GetFolderParID(theVRefNum, theDirID, &theDirID);
- }
- }
- if (theError == afpAccessDenied) {
- *inTrash = false;
- theError = noErr;
- }
- } else {
- if (theError == fnfErr) {
- theError = noErr;
- *inTrash = false;
- }
- }
- return theError;
- }
-
- pascal short GetFilesVolumeType(short aRefNum) {
- DCtlHandle theDCtl;
- StringPtr theDriverName;
- short theType;
- OSErr theError;
- short theVRefNum;
- HParamBlockRec pb;
-
- theError = GetVRefNum(aRefNum, &theVRefNum);
- if (theError == noErr) {
- pb.volumeParam.ioVolIndex = 0; /* use vRefNum */
- pb.volumeParam.ioVRefNum = theVRefNum;
- pb.volumeParam.ioNamePtr = nil; /* don't care about name */
- theError = PBHGetVInfoSync(&pb);
- }
- if (theError == noErr) {
- theDCtl = GetDCtlEntry(pb.volumeParam.ioVDRefNum);
- if (((**theDCtl).dCtlFlags & (1L << 6)) == 1) { /* RAM or ROM */
- /* RAM => dctlDriver is handle */
- theDriverName = (StringPtr)*((Handle)(**theDCtl).dCtlDriver);
- } else {
- /* ROM => dctlDriver is pointer */
- theDriverName = (StringPtr)(**theDCtl).dCtlDriver;
- }
- theDriverName += 18;
- if (EqualString(theDriverName, "\p.Sony", true, true)) {
- theType = GenericFloppy;
- } else if (EqualString(theDriverName, "\p.AFPTranslator", true,
- true)) {
- theType = GenericServer;
- } else if (EqualString(theDriverName, "\p.AppleCD", true,
- true)) {
- theType = GenericCDRom;
- } else if (EqualString(theDriverName, "\p.EDisk", true,
- true)) {
- theType = GenericRAMDisk;
- } else if (EqualString(theDriverName, "\p.SC/IOMEGA3.4", true,
- true)) {
- theType = IomegaBernoulliDisk;
- } else {
- theType = GenericHD;
- }
- } else {
- theType = GenericHD;
- }
- return theType;
- }
-
- pascal OSErr GetFilesVolumeStatus(short aRefNum, short *aStatus) {
- OSErr theError;
- short theVRefNum;
- HParamBlockRec pb;
-
- theError = GetVRefNum(aRefNum, &theVRefNum);
- if (theError == noErr) {
- pb.volumeParam.ioVolIndex = 0; /* use vRefNum */
- pb.volumeParam.ioVRefNum = theVRefNum;
- pb.volumeParam.ioNamePtr = nil; /* don't care about name */
- theError = PBHGetVInfoSync(&pb);
- }
- if (theError == noErr) {
- if ((pb.volumeParam.ioVDrvInfo > 0) &&
- (pb.volumeParam.ioVDRefNum < 0)) {
- *aStatus = online;
- } else if ((pb.volumeParam.ioVDrvInfo == 0) &&
- (pb.volumeParam.ioVDRefNum < 0)) {
- *aStatus = offline;
- } else if ((pb.volumeParam.ioVDrvInfo == 0) &&
- (pb.volumeParam.ioVDRefNum > 0)) {
- *aStatus = ejected;
- } else {
- *aStatus = online;
- }
- }
- return theError;
- }
-
- pascal OSErr GetFreeBytesOnVolume(short aVRefNum,
- unsigned long *aFreeBytes) {
- OSErr theError = noErr;
- ParamBlockRec pb;
-
- pb.volumeParam.ioNamePtr = nil; /* Don't care about name */
- pb.volumeParam.ioVRefNum = aVRefNum;
- pb.volumeParam.ioVolIndex = 0; /* Use vRefNum */
- theError = PBGetVInfoSync(&pb);
- if (theError == noErr) {
- *aFreeBytes =
- pb.volumeParam.ioVFrBlk * pb.volumeParam.ioVAlBlkSiz;
- }
- return theError;
- }
-
- pascal void FSGetFolderSize(FSSpecPtr aFolder,
- unsigned long *aFolderSize) {
- *aFolderSize = 0L; /* Start with nothing */
- (void)GetDirectorySize(aFolder->vRefNum, aFolder->parID,
- aFolderSize);
- }
-
- pascal OSErr GetDirectorySize(long aVRefNum, short aDirID,
- unsigned long *aDirectorySize) {
- static OSErr theError = noErr;
- static CInfoPBRec pb;
- short index = 1;
-
- #define kRequiredStackSpace (36)
- /* 36 bytes required on stack for each recursion */
- if (StackSpace() < kRequiredStackSpace)
- return insufficientStackErr;
-
- do {
- pb.dirInfo.ioDrDirID = aDirID;
- pb.dirInfo.ioVRefNum = aVRefNum;
- pb.dirInfo.ioNamePtr = nil;
- pb.dirInfo.ioFDirIndex = index;
- theError = PBGetCatInfoSync(&pb);
- if (theError == noErr) {
- if ((pb.dirInfo.ioFlAttrib & ioDirMask) != 0) {
- /*
- * we have a directory -- recurse
- */
- theError = GetDirectorySize(aVRefNum,
- pb.dirInfo.ioDrDirID, aDirectorySize);
- if (theError == fnfErr) {
- theError = noErr; /* clear error on way back */
- }
- } else {
- /*
- * we have a file -- add the physical length of it's forks
- */
- *aDirectorySize +=
- (pb.hFileInfo.ioFlPyLen + pb.hFileInfo.ioFlRPyLen);
- }
- }
- index++;
- } while (theError == noErr);
- return theError;
- }
-
- pascal OSErr GetTrashFolder(short aVRefNum, FSSpecPtr aTrash) {
- OSErr theError;
-
- theError =
- FindFolder(aVRefNum, kTrashFolderType, kDontCreateFolder,
- &(aTrash->vRefNum), &(aTrash->parID));
- return theError;
- }